const config    = require('../config/config');
const request   = require('request');
const crypto    = require('crypto');
class elves_plugin_login{

    constructor(username,password) {
        this.username = username;
        this.password = password;
    }

    async do(){
        return new Promise((resolve, reject) => {
            let options = {
                url:`https://www.sellersprite.com/v2/extension/signin?email=${this.username}&password=${crypto.createHash('md5').update(this.password).digest("hex")}&tk=705520.643986&version=2.8.2&language=zh_CN&extension=bfpnijhphaeccchggjmheoafglgldceb`,
                method:'GET',
                proxy:`http://${config.PROXY.HOST}:${config.PROXY.PORT}`,
                gzip:true,
                headers: {
                    "accept": "application/json",
                    "accept-encoding":"gzip, deflate, br",
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
                }
            }
            request(options,async (err,response,body) => {
                if(err){
                    console.log(`<<<<<<<>>>>>>>>> Elves plugin login failed.Error:${err}`);
                    reject(err.message);
                }else{
                    body = JSON.parse(body);
                    console.log(`Elves plugin login success. Token:${body.data.token}`);
                    resolve(body.data.token);
                }
            })
        })
    }

}

module.exports = elves_plugin_login;
